package dao.impl;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.hibernate.sql.Update;
import org.springframework.stereotype.Repository;

import pojo.Info;
import dao.InfoDao;

@Repository(value="infoDao")
public class InfoDaoImpl implements InfoDao{
	@PersistenceContext(name="un")
	private EntityManager em;

	@Override
	public Info addInfo(Info info) {
			em.persist(info);
			return info;
	}

	@Override
	public Info findInfo(String name,String pwd) {
		String jpql = "select i from Info i where i.name=:name and i.pwd=:pwd";
		List<Info> infos = em.createQuery(jpql)
								.setParameter("name", name)
								.setParameter("pwd", pwd)
								.getResultList();
		if(infos.isEmpty()){
			return null;
		}else{
			return infos.get(0);
		}
	}
	
	
	public int update(Info info){
		Info i = em.find(Info.class, info.getId());
		System.out.println(info.getAge());
		i.setAge(info.getAge());
		em.merge(i);
		return 1;
	}
}
